package com.xps.charminguser.view.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.custom.supereasydialog.view.EasyDialog;
import com.custom.supereasydialog.view.IEasyDialog;
import com.jude.utils.JUtils;
import com.xps.charminguser.R;
import com.xps.charminguser.bean.VerificationBean;
import com.xps.charminguser.bean.WaitOrderBean;
import com.xps.charminguser.date.net.UserNetWorks;
import com.xps.charminguser.utils.SharedPreferencesUtils;
import com.xps.charminguser.view.activity.AcceptOrderActivity;
import com.xps.charminguser.view.activity.SeeOrderActivity;
import com.xps.charminguser.view.base.BaseFragment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;

/**
 * Created by MI on 2018/1/10.
 */

public class ConductFragment extends BaseFragment {
    @BindView(R.id.Layout_WaitOrder)
    LinearLayout LayoutWaitOrder;
    Unbinder unbinder;
    TextView TextAddress;
    TextView TextTime;
    TextView TextPayment;
    TextView TextMoney;
    TextView TextDistribution;
    TextView TextTotal;
    TextView TextRemarks;
    TextView TextStore;
    TextView TextAccept;
    TextView TextState;

    @Override
    protected int getRes() {
        return R.layout.fragment_conduct;
    }

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        getWaitOrder();
    }

    @Override
    protected void setData(View view) {

    }

    @Override
    public void onClick(View v) {

    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    public void getWaitOrder() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getWaitOrder(user_token, new Subscriber<WaitOrderBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(WaitOrderBean waitOrderBean) {
                LayoutWaitOrder.removeAllViews();
                List<WaitOrderBean.ReturnBodyBean> list = waitOrderBean.getReturn_body();
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        addOrderItem(list.get(i), LayoutWaitOrder);
                        addDividerLine(LayoutWaitOrder);
                    }
                }
            }
        });
    }

    ImageView Image_Telephone;

    public void addOrderItem(final WaitOrderBean.ReturnBodyBean dataBean, LinearLayout ll_lv) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.adapter_waitoeder, null);

        TextAddress = view.findViewById(R.id.Text_Address);
        TextTime = view.findViewById(R.id.Text_Time);
        TextPayment = view.findViewById(R.id.Text_Payment);
        TextMoney = view.findViewById(R.id.Text_Money);
        TextDistribution = view.findViewById(R.id.Text_Distribution);
        TextTotal = view.findViewById(R.id.Text_Total);
        TextRemarks = view.findViewById(R.id.Text_Remarks);
        TextStore = view.findViewById(R.id.Text_Store);
        TextState = view.findViewById(R.id.Text_State);
        TextAccept = view.findViewById(R.id.Text_Accept);
        TextAddress.setText(dataBean.getOrder_address());
        Image_Telephone = view.findViewById(R.id.Image_Telephone);
        parseTime(dataBean.getOrder_create_time(), TextTime);
        if (dataBean.getOrder_payment_method().equals("0")) {
            TextPayment.setText("现金");
        } else if (dataBean.getOrder_payment_method().equals("1")) {
            TextPayment.setText("刷卡");
        }
        TextMoney.setText(dataBean.getOrder_money());
        TextDistribution.setText(dataBean.getOrder_fare());
        TextTotal.setText(dataBean.getOrder_total());
        TextRemarks.setText(dataBean.getOrder_remarks());
        TextStore.setText(dataBean.getOrder_after_store());
        TextAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AcceptOrderActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("dataBean", dataBean);
                getActivity().startActivity(intent);
            }
        });
        Image_Telephone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diallPhone(dataBean.getOrder_tel());
            }
        });
        TextState.setText("等待接单");
        ll_lv.addView(view);
    }

    public void diallPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    public void addDividerLine(LinearLayout ll_lv) {
        View v = new View(getActivity());
        v.setAlpha(0.5f);
        v.setBackgroundColor(getActivity().getResources().getColor(R.color.dialogplus_card_shadow));
        v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        ll_lv.addView(v);
    }

    @Override
    public void onResume() {
        super.onResume();
        getWaitOrder();
    }

    /**
     * 解析时间
     */
    void parseTime(String createTime, TextView tvOrderTime) {
        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        tvOrderTime.setText(simpleDateFormat.format(passDate));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
